package com.ruoyi.restaurant.service.impl;

import java.util.List;

import com.ruoyi.restaurant.domain.RestDish;
import com.ruoyi.restaurant.domain.dto.RestOrdersDTO;
import com.ruoyi.restaurant.service.IRestDishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.restaurant.mapper.RestOrdersMapper;
import com.ruoyi.restaurant.domain.RestOrders;
import com.ruoyi.restaurant.service.IRestOrdersService;

/**
 * 订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-30
 */
@Service
public class RestOrdersServiceImpl implements IRestOrdersService 
{
    @Autowired
    private RestOrdersMapper restOrdersMapper;

    @Autowired
    private IRestDishService iRestDishService;

    /**
     * 查询订单
     * 
     * @param orderId 订单主键
     * @return 订单
     */
    @Override
    public RestOrders selectRestOrdersByOrderId(Long orderId)
    {
        return restOrdersMapper.selectRestOrdersByOrderId(orderId);
    }

    @Override
    public List<RestOrders> selectRestOrdersList(RestOrders restOrders) {
        return restOrdersMapper.selectRestOrdersList(restOrders);
    }

    /**
     * 查询订单列表
     * @return 订单
     */
    @Override
    public List<RestOrdersDTO> selectRestOrdersListWithName()
    {
        return restOrdersMapper.findOrdersWithDishName();
    }

    /**
     * 新增订单
     * 
     * @param restOrders 订单
     * @return 结果
     */
    @Override
    public int insertRestOrders(RestOrders restOrders)
    {
        Long dishId = restOrders.getDishId();
        RestDish restDish = iRestDishService.selectRestDishById(dishId);
        restDish.setStock(restDish.getStock() - restOrders.getQuantity());
        iRestDishService.updateRestDish(restDish);
        return restOrdersMapper.insertRestOrders(restOrders);
    }

    /**
     * 修改订单
     * 
     * @param restOrders 订单
     * @return 结果
     */
    @Override
    public int updateRestOrders(RestOrders restOrders)
    {
        Long dishId = restOrders.getDishId();
        Long orderId = restOrders.getOrderId();
        RestOrders restOrdersPrev = this.selectRestOrdersByOrderId(orderId);
        Long quantityPrev = restOrdersPrev.getQuantity();
        Long quantity = restOrders.getQuantity();
        RestDish restDish = iRestDishService.selectRestDishById(dishId);
        restDish.setStock(restDish.getStock() + quantityPrev - quantity);
        iRestDishService.updateRestDish(restDish);
        return restOrdersMapper.updateRestOrders(restOrders);
    }

    /**
     * 批量删除订单
     * 
     * @param orderIds 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteRestOrdersByOrderIds(Long[] orderIds)
    {
        return restOrdersMapper.deleteRestOrdersByOrderIds(orderIds);
    }

    /**
     * 删除订单信息
     * 
     * @param orderId 订单主键
     * @return 结果
     */
    @Override
    public int deleteRestOrdersByOrderId(Long orderId)
    {
        return restOrdersMapper.deleteRestOrdersByOrderId(orderId);
    }
}
